//
// Created by denglibin on 2021/6/3.
//
#include "global.h"
#include "SDL_image.h"
#include "stdio.h"
#include "math.h"
/**
 * 全局事件处理
 * @param gameWindow
 */
void GlobalHandleEvent(GameWindow* gameWindow){

    if(gameWindow->event.type == SDL_QUIT){
       gameWindow->run = 0;
    }
    if(gameWindow->event.type == SDL_KEYDOWN){
        switch (gameWindow->event.key.keysym.sym) {
            case SDLK_ESCAPE:
                gameWindow->run = 0;
                break;
            case SDLK_1:
                gameWindow->scene_index = 0; //场景切换
                break;
            case SDLK_2:
                gameWindow->scene_index = 1; //场景切换
                break;
            default:
                break;
        }
    }
}

/**
 * 精灵绘制
 * @param gameWindow
 * @param mainSpirit
 */
void  GlobalDrawSpirit(GameWindow* gameWindow, MainSpirit* mainSpirit){
    //跳跃
    if(mainSpirit->jumping){
        uint32_t  now = SDL_GetTicks();
        int t = (int)(now - mainSpirit->jump_start_time);

        int s = (160 * t - 40 * t/10 * t/10)/100;
        mainSpirit->y = mainSpirit->jump_start_y - s;

        if(mainSpirit->y >= mainSpirit->jump_start_y && t > 0){
            mainSpirit->jumping = 0;
            mainSpirit->y = mainSpirit->jump_start_y;
        }
    }
    //移动
    mainSpirit->map_x +=  mainSpirit->move_direction * mainSpirit->speed* gameWindow->interval/1000;
    if(mainSpirit->map_x <= 0){
        mainSpirit->map_x = 0;
    }
    if(mainSpirit->map_x  >= mainSpirit->map_w - mainSpirit->w ){
        mainSpirit->map_x = mainSpirit->map_w - mainSpirit->w;
    }
    mainSpirit->x = GlobalGetWinXByMapX(gameWindow, mainSpirit->map_x, mainSpirit->map_w);
    //设置渲染器颜色
    SDL_SetRenderDrawColor(gameWindow->winRender, 0, 255, 0, 255);
    SDL_Rect distRect = {mainSpirit->x, mainSpirit->y, mainSpirit->w, mainSpirit->h };
    SDL_RenderFillRect(gameWindow->winRender, &distRect);
    //SDL_RenderCopy(gameWindow->winRender, mainSpirit->BillRizer_texture, &srcRect, &distRect);

}

/**
 * 根据在地图中的x坐标计算在窗口中的x坐标
 * @param gameWindow
 * @param mapX
 * @param int mapW
 * @return
 */
int GlobalGetWinXByMapX(GameWindow* gameWindow, int mapX, int mapW){
    if(mapX <= gameWindow->win_w/2){
        return mapX;
    }
    if(mapX > mapW - gameWindow->win_w/2){
        return gameWindow->win_w/2 + mapX -(mapW - gameWindow->win_w/2);
    }
    return gameWindow->win_w/2;
}

/**
 * 根据图片获取纹理
 * @param img
 * @param renderer
 * @return
 */
SDL_Texture * GlobalCreateTexture(const char* img, SDL_Renderer* renderer){
    SDL_Surface  * surface = IMG_Load(img);
    if (surface == NULL){
        SDL_Log("Can not load image %s\n", SDL_GetError());
        exit(1);
    }
    SDL_Texture* texture  = SDL_CreateTextureFromSurface(renderer, surface);
    if (texture == NULL){
        SDL_Log("Can not create texture %s\n", SDL_GetError());
        exit(1);
    }
    //获取宽高
    //SDL_QueryTexture(texture, NULL, NULL, &w, &h);
    SDL_FreeSurface(surface);//清理
    return texture;
}